//
//  Cwgvc.m
//  ainkjj
//
//  Created by MAX on 2020/10/12.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
/*
 NSString *device_id = @"IF-YA-SPC-20A-FCF5C459F284-7800";

 NSString *type = @"YA-SPC-20A";
 NSString *mac =  @"C4-4F-33-14-E4-AD";

 */

#import "Cwgvc.h"

#import "Device_mqtt_publish_model.h"
#import "Device_mqtt_sub_model.h"
#import "Shebei_offline_popView.h"
#import "MQTTLog.h"
#import "MQTTSessionManager.h"
#import <FCUUID/FCUUID.h>

#import "App_devices_model.h"
#import "App_deviceInfo.h"
#import "Tongfeng_xiaodu_vc.h"
#import "Zhire_vc.h"
#import "Shui_shi_vc.h"


@interface Cwgvc ()<MQTTSessionManagerDelegate>

@property (nonatomic, copy) NSString *ainankang_app_topic;
@property (nonatomic, copy) NSString *ainankang_dev_topic;


@property (nonatomic, strong)MQTTSessionManager *sessionManager;
@property (nonatomic, copy) NSString *login_report_MAC;
@property (nonatomic, copy) NSString *login_report_dev_type;
@property (nonatomic, strong)NSTimer *check_handshake_timer;
@property (nonatomic, assign)BOOL handshake_yes;

@property (nonatomic, strong)Zhire_vc *revc;
@property (nonatomic, strong)Shui_shi_vc *shui_vc;
@property (nonatomic, strong)Tongfeng_xiaodu_vc *feng_vc;

@end

@implementation Cwgvc

- (IBAction)shui:(id)sender {
    NSMutableArray *dataArr = [NSMutableArray array];
    
    [dataArr addObject:@{@"reg":@"drinking_current",
                         @"value" : @(self.shui_tf.text.intValue)
    }];

    [self queryWith_cmd:@"dev" data:dataArr];
}
- (IBAction)shi:(id)sender {
    NSMutableArray *dataArr = [NSMutableArray array];
    
    [dataArr addObject:@{@"reg":@"feed_current",
                         @"value" : @(self.shi_tf.text.intValue)
    }];
    [self queryWith_cmd:@"dev" data:dataArr];
}
- (IBAction)feng:(id)sender {
    NSMutableArray *dataArr = [NSMutableArray array];
    
    [dataArr addObject:@{@"reg":@"venting",
                         @"value" : @(self.feng_tf.text.intValue * 60)
    }];
    [self queryWith_cmd:@"dev" data:dataArr];
}
- (IBAction)du:(id)sender {
    NSMutableArray *dataArr = [NSMutableArray array];
    
    [dataArr addObject:@{@"reg":@"sterilize_current",
                         @"value" : @(self.du_tf.text.intValue  * 60)
    }];
    [self queryWith_cmd:@"dev" data:dataArr];
}
- (IBAction)re:(id)sender {
    NSMutableArray *dataArr = [NSMutableArray array];
    
    [dataArr addObject:@{@"reg":@"warm",
                         @"value" : @(self.re_tf.text.intValue  * 60)
    }];
    [self queryWith_cmd:@"dev" data:dataArr];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [MBProgressHUD hideHUD];
    
//    [self.check_handshake_timer invalidate];
//    self.check_handshake_timer = nil;
    
}
- (void)dealloc{
    
    
    if (self.deviceInfo.data.is_virtual == NO) {// 虚拟设备后面的不再执行了
        return;
    }
    [self.sessionManager disconnectWithDisconnectHandler:^(NSError *error) {
        if (error) {
            DLog(@"%@",error);
        }else{
            [self.sessionManager requiresTearDown];
        }
    }];
        
    DLog(@"(づ￣3￣)づ╭❤～庆祝");
}
- (void)viewDidLoad{
    [super viewDidLoad];
    //
    self.title = @"智能宠物柜";
    self.login_report_MAC = @"FC-F5-C4-59-F2-84";
    self.deviceInfo = [[App_deviceInfo alloc] init];
    
    self.deviceInfo.data = [App_deviceInfoData new];;
    self.deviceInfo.data.device_id = @"IF-YA-SPC-20A-FCF5C459F284-7800";
    self.deviceInfo.data.device_type = @"YA-SPC-20A";
    
    if ([Anktool strIsBlank:self.deviceInfo.data.device_id]) {
        DLog(@"发生错误，无设备id");
        
    }else{
        self.ainankang_app_topic = [@"ainankang_app/" stringByAppendingString:self.deviceInfo.data.device_id];
        self.ainankang_dev_topic = [@"ainankang_dev/" stringByAppendingString:self.deviceInfo.data.device_id];
    }
   
    [self mqtt_manager];

    // 去皮
    NSArray *t = @[@"整体去皮", @"喂食去皮", @"喂水去皮"];
    for ( NSInteger i = 0; i < 3; i++) {
        QMUIButton *b = [QDUIHelper generateLightBorderedButton];
        [self.view addSubview:b];
        b.tag = i;
        b.frame = CGRectMake( (ScreenWidth * 0.33 ) * i + 10, ScreenHeight - (Lzy_TabbarSafeBottomMargin + 60), self.view.mj_w * 0.33 -20, 44);
        [b setTitle:t[i] forState:UIControlStateNormal];
        [b addTarget:self action:@selector(qupi:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    UITextView *textv = UITextView.new;
    [self.view addSubview:textv];
    textv.frame = CGRectMake(10, ScreenHeight - (Lzy_TabbarSafeBottomMargin + 60 + 64), ScreenWidth - 20, 60);
    textv.editable = NO;
    textv.text = @"    请保持智能宠物柜正常入网后操作去皮。\n    智能宠物柜，在被拆装被移动后，猫不在柜中时，点击整体去皮。\n    清空食物、水的容器后，手动进行喂食喂水的去皮，有利于保证喂食喂水量的精准，建议定期进行喂食喂水去皮。";
}
- (void)qupi:(QMUIButton *)sender{
    /*
     NSMutableArray *dataArr = [NSMutableArray array];
     
     [dataArr addObject:@{@"reg":@"feed_current",
     @"value" : @(self.shi_tf.text.intValue)
     }];
     
     {"dev_id":"IF-YA-SPC-20A-FCF5C459F284-7800","dev_type":"YA-SPC-20A","cmd": "sensor","index": 52,"data": [{"reg": "weighing_water_zero"}]}
     {"dev_id":"IF-YA-SPC-20A-FCF5C459F284-7800","dev_type":"YA-SPC-20A","cmd": "sensor","index": 52,"data": [{"reg": "weighing_water_gap","value": 8.42}]}
     
     {"dev_id":"IF-YA-SPC-20A-FCF5C459F284-7800","dev_type":"YA-SPC-20A","cmd": "sensor","index": 52,"data": [{"reg": "weighing_all_zero"}]}
     {"dev_id":"IF-YA-SPC-20A-FCF5C459F284-7800","dev_type":"YA-SPC-20A","cmd": "sensor","index": 52,"data": [{"reg": "weighing_all_gap","value": 8.42}]}
     
     {"dev_id":"IF-YA-SPC-20A-FCF5C459F284-7800","dev_type":"YA-SPC-20A","cmd": "sensor","index": 52,"data": [{"reg": "weighing_food_zero"}]}
     {"dev_id":"IF-YA-SPC-20A-FCF5C459F284-7800","dev_type":"YA-SPC-20A","cmd": "sensor","index": 52,"data": [{"reg": "weighing_food_gap","value": 8.42}]}
     */
    NSMutableArray *dataArr = [NSMutableArray array];

    switch (sender.tag) {
        case 0:
            [dataArr addObject:@{@"reg":@"weighing_all_zero"}];
            break;
            
        case 1:
            [dataArr addObject:@{@"reg":@"weighing_food_zero"}];
            break;
            
        case 2:
            [dataArr addObject:@{@"reg":@"weighing_water_zero"}];
            break;
            
    }
    
    [self queryWith_cmd:@"sensor" data:dataArr];

}
#pragma mark - ================== mqtt manager ==================
- (void)mqtt_manager{
    
    if (!self.sessionManager) {
        self.sessionManager = [[MQTTSessionManager alloc] init];
        self.sessionManager.delegate = self;
        [MQTTLog setLogLevel:DDLogLevelWarning];
        
        /**
         host: 服务器地址
         port: 服务器端口
         tls:  是否使用tls协议，mosca是支持tls的，如果使用了要设置成true
         keepalive: 心跳时间，单位秒，每隔固定时间发送心跳包, 心跳间隔不得大于120s
         clean: session是否清除，这个需要注意，如果是false，代表保持登录，如果客户端离线了再次登录就可以接收到离线消息
         auth: 是否使用登录验证
         user: 用户名
         pass: 密码
         willTopic: 订阅遗嘱主题
         willMsg: 自定义的离线消息
         willQos: 接收离线消息的级别
         clientId: 客户端id，需要特别指出的是这个id需要全局唯一，因为服务端是根据这个来区分不同的客户端的，默认情况下一个id登录后，假如有另外的连接以这个id登录，上一个连接会被踢下线, 我使用的设备UUID
         */
        NSString *clientId = [NSString stringWithFormat:@"%@%@", [FCUUID uuidForDevice], AuthSignin_model.sharedInstance.data.user_info.mobile];
        //        [self.sessionManager setSubscriptions:@{@"TopicApp/DeviceId" : @(MQTTQosLevelExactlyOnce)}];
        
        
        
        // 订阅主题    NSDictionary类型，Object 为 QoS，key 为 Topic
        NSMutableDictionary *subTopic = [NSMutableDictionary dictionary];
        [subTopic setObject:@(MQTTQosLevelAtMostOnce) forKey:@"ainankang_dev"];// 公共主题，握手之前，设备发出来的
        if ([Anktool strIsBlank:self.ainankang_dev_topic]) {
            DLog(@"设备id为空");
        }else{
            [subTopic setObject:@(MQTTQosLevelAtMostOnce) forKey:self.ainankang_dev_topic];
        }
        self.sessionManager.subscriptions = subTopic;
        
        MQTTSSLSecurityPolicy *sp = [MQTTSSLSecurityPolicy policyWithPinningMode:MQTTSSLPinningModeNone];
        
        [self.sessionManager connectTo:@"47.97.127.229"
                                  port:1883
                                   tls:false
                             keepalive:60  //心跳间隔不得大于120s
                                 clean:true
                                  auth:true
                                  user:@"ainankang_user01"
                                  pass:@"ainankang@01"
                                  will:NO
                             willTopic:nil
                               willMsg:nil
                               willQos:0
                        willRetainFlag:false
                          withClientId:clientId
                        securityPolicy:sp
                          certificates:nil
                         protocolLevel:MQTTProtocolVersion311
                        connectHandler:^(NSError *error) {
            
            if (error) {
                DLog(@"error:%@", error);
            }else{
                #pragma mark - ================== 链接成功后，发送握手消息 ==================
                [self send_handshake_msg];
            }
            
        }];
        
        
    }else{
        /*
         MQTTSessionManagerStateStarting,
         MQTTSessionManagerStateConnecting,
         MQTTSessionManagerStateError,
         MQTTSessionManagerStateConnected,
         MQTTSessionManagerStateClosing,
         MQTTSessionManagerStateClosed
         */
        if (self.sessionManager.state == MQTTSessionManagerStateConnected) {
                /* TODO: #待完成# 可以发送消息*/
            
        }else{
            [self.sessionManager connectToLast:^(NSError *error) {
                
                if (error) {
                    DLog(@"error:%@", error);
                }else{
                    /* TODO: #待完成# 可以发送消息*/
                    
                }
            }];
        }
        
        
    }
    
//    [self check_handshake];
    
}
#pragma mark - ================== 发 ==================
- (void)setHandshake_yes:(BOOL)handshake_yes{
    if (_handshake_yes == NO && handshake_yes == YES) {
            /* TODO: #待完成# 握手成功，可以发消息*/
    }
    _handshake_yes = handshake_yes;
    if (handshake_yes == YES) {
        [MBProgressHUD hideHUD];
    }
}
- (void)send_handshake_msg{
    
    //    [MBProgressHUD showActivityMessageInView:@"加载中"];
    NSMutableArray *dataArr = [NSMutableArray array];
    
    [dataArr addObject:@{@"reg":@"MAC",
                         @"value" : self.login_report_MAC
    }];
    [self queryWith_cmd:@"handshake" data:dataArr];
    
    
    
}


- (void)queryWith_cmd:(NSString *)cmd data:(NSArray *)dataArr{

    Device_mqtt_publish_model *pub_msg = [Device_mqtt_publish_model new];
    
    pub_msg.dev_id = self.deviceInfo.data.device_id;
    pub_msg.dev_type = self.deviceInfo.data.device_type;//@"YA-HBB-19A" self.deviceInfo.data.device_type
    pub_msg.index = @(++AuthSignin_model.sharedInstance.msgIndex).stringValue;
    
    pub_msg.cmd = cmd;
    
    pub_msg.data = dataArr.copy;
    
    NSString *pub_msg_str = [[pub_msg mj_JSONString] stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    DLog(@"%@", pub_msg_str);
    
    // {"cmd":"handshake","data":[{"reg":"MAC","value":"C44F3314ECF5"}],"dev_id":"IF-YA-HBB-19A-C44F3314ECF5-6300","dev_type":"YA-HBB-19A","index":0}
    [self.sessionManager
     sendData:[pub_msg_str dataUsingEncoding:NSUTF8StringEncoding] //要发送的消息体
     topic:self.ainankang_app_topic //要往哪个topic发送消息
     qos:0 //消息级别
     retain:false];
}

#pragma mark - ================== 收 ==================

- (void)sessionManager:(MQTTSessionManager *)sessionManager
     didReceiveMessage:(NSData *)data
               onTopic:(NSString *)topic
              retained:(BOOL)retained{
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        
        Device_mqtt_publish_model *model = [Device_mqtt_publish_model mj_objectWithKeyValues:data];
        if (![model.dev_id isEqualToString:self.deviceInfo.data.device_id]) {
            //            DLog(@"mqtt消息与当前deviceId不一致 %@ %@", model.dev_id, self.deviceInfo.data.device_id);
            return;
        }
        if ([topic isEqualToString:@"ainankang_dev"]) {
            // 设备的 {"dev_id":"IF-YA-HBB-19A-C44F3314ECF5-6300","dev_type":"YA-HBB-19A","cmd":"login_report","data":[{"reg":"MAC","value":"C4-4F-33-14-EC-F5"},{"reg":"IP","value":"192.168.1.14"}]}
            
            
            if ([model.cmd isEqualToString:@"login_report"]) {
                if (!self.login_report_MAC) {
                    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                    DLog(@"data:%@ topic:%@", str, topic);
                    for (NSDictionary *dic in model.data) {
                        NSString *reg = [dic objectForKey:@"reg"];
                        if ([reg isEqualToString:@"MAC"]) {
                            NSString *value = [dic objectForKey:@"value"];
                            self.login_report_MAC = value;
                            self.login_report_dev_type = model.dev_type;
                            [self send_handshake_msg];
                        }
                        
                    }
                    
                }
            }else if ([model.cmd isEqualToString:@"handshake_res"]) {
                // topic:ainankang_dev {"dev_id":"IF-YA-HBB-19A-C44F3314ECF5-6300","data":[{"reg":"handshake","value":0}],"dev_type":"YA-HBB-19A","cmd":"handshake_res","index":"1","index":0}
                NSDictionary *handshake = model.data.firstObject;
                if ([[handshake objectForKey:@"reg"] isEqualToString:@"handshake"]) {
                    NSNumber *value = [handshake objectForKey:@"value"];
                    if (value.integerValue == 0) {
                        DLog(@"1----------->握手成功 ");
                        self.handshake_yes = YES;
                        
                        
                    }
                }
            }
            
            
        }else if([topic isEqualToString:self.ainankang_dev_topic]){
            // 握手后，设备 主题
            
            if(![model.cmd isEqualToString:@"weighing_get_active"] && ![model.cmd isEqualToString:@"sensor_data"]){
                NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                DLog(@"屏蔽了sensor_data和weighing_get_active。\n data:%@ topic:%@", str, topic);
            }
            
            
            Device_mqtt_publish_model *model = [Device_mqtt_publish_model mj_objectWithKeyValues:data];
            
            if ([model.cmd isEqualToString:@"dev_execute"] || [model.cmd isEqualToString:@"dev"]) {
                
                for (NSDictionary *dict in model.data) {
                        /* TODO: #待完成# */
                }
                
            }else if ([model.cmd isEqualToString:@"dev_active"]) {
                
                for (NSDictionary *dict in model.data) {
                    /* TODO: #待完成# */
                }
                
            }else if([model.cmd isEqualToString:@"handshake_res"]){
                // "value": 0/1~255   succeed/failed。示例  {"reg":"handshake","value":0}
                NSDictionary *handshake = model.data.firstObject;
                if ([[handshake objectForKey:@"reg"] isEqualToString:@"handshake"]) {
                    NSNumber *value = [handshake objectForKey:@"value"];
                    if (value.integerValue == 0) {
                        DLog(@"1----------->握手成功 ");
                        self.handshake_yes = YES;
                        
                        
                    }
                }
            }else if([model.cmd isEqualToString:@"audio_execute"]){
                // audio_execute
                for (NSDictionary *dict in model.data) {
                    /* TODO: #待完成# */
                }
            }else if([model.cmd isEqualToString:@"audio"]){
                // audio
                for (NSDictionary *dict in model.data) {
                    /* TODO: #待完成# */
                }
            }else if([model.cmd isEqualToString:@"audio_active"]){
                // audio_active
                for (NSDictionary *dict in model.data) {
                    /* TODO: #待完成# */
                }
            }else if([model.cmd isEqualToString:@"off_line"]){
                // off_line {"dev_id":"IF-YA-HBB-19A-C44F3314ECF5-6300","dev_type":"YA-HBB-19A","cmd":"off_line","data":[{"reg":"time","value":"2020-03-19 13:35:41"}]}
                
                NSDictionary *dict = model.data.firstObject;
                NSString *reg = [dict objectForKey:@"reg"];
                NSString *value = [dict objectForKey:@"value"];
                if ([reg isEqualToString:@"time"] && ![Anktool strIsBlank:value]) {
                    
                    
                    
                    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init ];
                    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                    NSDate * d1 = [dateFormatter dateFromString:value];
                    
                    
                    
                    NSTimeZone * zone = [NSTimeZone systemTimeZone];
                    
                    // 将偏差的时间加到原来的时间上就是正式的时间
                    d1 = [d1 dateByAddingTimeInterval:[zone secondsFromGMTForDate:d1]];
                    

                    
                    NSDate * d3 =  [NSDate dateWithTimeIntervalSinceNow:8 * 60 * 60];
                    
                    NSTimeInterval t2 = [d3 timeIntervalSinceDate:d1];
                    
                    if (t2 > 120) {// 超过120s的遗嘱消息，不处理
                        DLog(@"超过120s的遗嘱消息，不处理");
                        return;
                    }
                    
                    //[MBProgressHUD showTipMessageInView:[NSString stringWithFormat:@"%@设备掉线", value]];
                    self.handshake_yes = NO;
                   
                    // 重置状态
                    self.login_report_MAC = self.login_report_dev_type = nil;
                    
                }
                
            }else if([model.cmd isEqualToString:@"weighing_get_active"]){
                // {"dev_id":"IF-YA-HBB-19A-C44F3314ECF5-6300","dev_type":"YA-HBB-19A","cmd":"weighing_get_active","index":0,"data":[{"reg":"weight","value":2147483647},{"reg":"gap","value":0}]}
                //            for (NSDictionary *dict in model.data) {
                //                [self handle_audio_active_reg_dict:dict];
                //            }
                //                DLog(@"有连接");
                self.handshake_yes = YES;
                
                
            }else if([model.cmd isEqualToString:@"sensor_data"]){
                
                for (NSDictionary *dict in model.data) {
                    NSString *reg = dict[@"reg"];
                    NSUInteger value = [dict[@"value"] integerValue];
                    if ([reg isEqualToString:@"TEM_ENV"]) {
//                        self.guanaiVc.huanjing_wendu_lb.text = [Anktool wendu:value];
                    }else if ([reg isEqualToString:@"TEM_QUILT1"]){
                        //                    self.guanaiVc.beigan_wendu_lb.text = [NSString stringWithFormat:@"%lu℃", (unsigned long)value];
                    }else if ([reg isEqualToString:@"TEM_QUILT2"]){
//                        self.guanaiVc.beigan_wendu_lb.text = [Anktool wendu:value];
                        
                    }else if ([reg isEqualToString:@"TEM_IN"]){}else if ([reg isEqualToString:@"RT_NS"]){}
                }
            }else if ([model.cmd isEqualToString:@"login_report"]){
                self.handshake_yes = YES;
                
                
            }
        }else{}// 除约定外的其他主题
        
    });
}


- (void)sessionManager:(MQTTSessionManager *)sessionManager didChangeState:(MQTTSessionManagerState)newState{
    NSString *str;
    switch (newState) {
        case MQTTSessionManagerStateStarting:
            str = @"正在启动";
            break;
            
        case MQTTSessionManagerStateConnecting:
            str = @"正在连接";
            break;
        case MQTTSessionManagerStateError:
            str = @"发生错误";
            break;
        case MQTTSessionManagerStateConnected:
            str = @"连接成功";
            break;
        case MQTTSessionManagerStateClosing:
            str = @"正在关闭";
            break;
        case MQTTSessionManagerStateClosed:
            str = @"已经关闭";
            break;
    }
    DLog(@"%@", str);
}
#pragma mark - ================== 懒加载 ==================
- (instancetype)init

{
    self = [super init];
    if (self) {
        
        self.titleColorSelected = appMainColor;
        self.titleColorNormal = UIColor.darkGrayColor;
        self.titleSizeNormal = 15;
        self.titleSizeSelected = 16;
        [self setMenuViewStyle:WMMenuViewStyleLine];
        [self setAutomaticallyCalculatesItemWidths:YES];
//        self.hidesBottomBarWhenPushed = YES;
////        self.entity = list;
//        self.titleColorSelected = themeColor;
//        self.titleColorNormal = rgb(153,153,153);
//        self.titleSizeNormal = 17;
//        self.titleSizeSelected = 20;
//        [self setMenuViewStyle:WMMenuViewStyleLine];
//        self.progressWidth = 40;
//        self.keys = @[@"category_id", @"category_id"].mutableCopy;
//        self.values = @[@"customermini/taskplerank", @"customermini/beanrank"].mutableCopy;
//        [self reloadData];
    }
    
    return self;
}

- (Zhire_vc *)revc{
    if(_revc == nil) _revc = [[Zhire_vc alloc] init];
    return _revc;
}
- (Shui_shi_vc *)shui_vc{
    if(_shui_vc == nil) _shui_vc = [[Shui_shi_vc alloc] init];
    return _shui_vc;
}
- (Tongfeng_xiaodu_vc *)feng_vc{
    if(_feng_vc == nil) _feng_vc = [[Tongfeng_xiaodu_vc alloc] init];
    return _feng_vc;
}
- (void)check_handshake{
    self.check_handshake_timer =  [NSTimer timerWithTimeInterval:25 repeats:NO block:^(NSTimer * _Nonnull timer) {
        if (!self.handshake_yes) {
            [self shebei_offline_popView];
        }
    }];
    [[NSRunLoop currentRunLoop] addTimer:self.check_handshake_timer forMode:NSDefaultRunLoopMode];
    
}
- (void)shebei_offline_popView{
    __weak __typeof(self) weakSelf = self;
    MMPopupCompletionBlock btnBlock = ^(MMPopupView *popupView, BOOL finished){
        
        DLog(@"%d", finished);
        if (finished && !weakSelf.handshake_yes) {
            if (self.login_report_MAC.length > 0) {
                [weakSelf send_handshake_msg];
            }
        }
    };
    
    Shebei_offline_popView *pinView = [Shebei_offline_popView new];
    pinView.img_name = @"kz_failed_icon";
    pinView.tipStr = @"未连接到设备，请重试";
    pinView.queren_btn_title_str = @"重试";
    pinView.btnBlock = btnBlock;
    [pinView show];
}

- (NSMutableArray *)titlesArr{
    if(_titlesArr == nil)
    { _titlesArr = @[@"喂食喂水", @"通风消毒", @"宠窝制热"].mutableCopy;
    }
    
    return _titlesArr;
}


#pragma mark - WMPageController DataSource
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.titlesArr.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return self.titlesArr[index];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    UIViewController *vc = nil;
    switch (index) {
        case 0:
            vc = self.shui_vc;
            break;
            
        case 1:
            vc = self.feng_vc;
            break;
            
        case 2:
            vc = self.revc;
            break;
            
        default:
            vc = [QDCommonViewController new];
            break;
    }
    return vc;
}
-(CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView{
    CGFloat originY =  CGRectGetMaxY(self.navigationController.navigationBar.frame);
    
    return  CGRectMake(0,  originY, [UIScreen mainScreen].bounds.size.width, 50);
}
- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView{
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    //    originY = Lzy_StatusBarAndNavigationBarHeight;
    return  CGRectMake(0, originY, self.view.bounds.size.width,self.view.bounds.size.height - originY - 100);
    
}

@end
